package cm.day12;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
class MyRun implements Runnable {
    private int employeeNum = 100;//员工人数，也是编号
    private int frontNum = 0;//前门人数
    private int backNum = 0;//后门人数

    public void run() {
        while (employeeNum > 0) {
            synchronized (this) {
                count();
            }
        }
    }

    public void count() {
        List<Integer> employeeNumList = new ArrayList<>();//存储员工编号
        List<String> ballList = new ArrayList<>();//存储双色球
        String ball = create();

        try {
            Thread.sleep(50);
        } catch (Exception e) {
            System.out.println(e);
        }
        if (!employeeNumList.contains(employeeNum) && !ballList.contains(ball)) {
            ballList.add(ball);
            employeeNumList.add(employeeNum);

            if (employeeNum == 0) {
                System.out.println("从后门入场的员工总共：" + backNum);
                System.out.println("从前门入场的员工总共：" + frontNum);
                return;
            }
            String name = Thread.currentThread().getName();//获取线程名
            if (name.equals("前门")) {
                System.out.println("编号为: " + employeeNum + " 的员工 从" + name + " 入场！拿到的双色球号码是：[" + ball + "]");
                frontNum++;
            } else if (name.equals("后门")) {
                System.out.println("编号为: " + employeeNum + " 的员工 从" + name + " 入场！拿到的双色球号码是：[" + ball + "]");
                backNum++;
            }
            employeeNum--;
        }
    }

    public  String create() {
        String[] red = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33"};
        //创建蓝球
        String[] blue = "01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16".split(",");
        boolean[] used = new boolean[red.length];
        Random r = new Random();
        String[] all = new String[7];
        for (int i = 0; i < 6; i++) {
            int idx;
            do {
                idx = r.nextInt(red.length);//0‐32
            } while (used[idx]);//如果使用了继续找下一个
            used[idx] = true;//标记使用了
            all[i] = red[idx];//取出一个未使用的红球
        }
        all[all.length - 1] = blue[r.nextInt(blue.length)];
        Arrays.sort(all);
        return Arrays.toString(all);
    }
}
public class Test6 {
    public static void main(String[] args) {
        MyRun mr1=new MyRun();
        MyRun mr2=new MyRun();
        new Thread(mr1,"前门").start();
        new Thread(mr2,"后门").start();

    }
}
